﻿@{
    ViewBag.Title = "Danh sách chai";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('ProductController', function ($scope, $compile, $http, service) {
        $scope.loadCategory = function () {
            var url = '@url' + '@Url.Action("CategoryApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $scope.categories.push({ "name": data[i].CategoryName, "value": data[i].CategoryID });
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.loadPartner = function () {
            var url = '@url' + '@Url.Action("PartnerApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $scope.partners.push({ "name": data[i].PartnerName, "value": data[i].PartnerID });
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.loadLot = function () {
            var url = '@url' + '@Url.Action("LotApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $scope.lots.push({ "name": data[i].LotCode, "value": data[i].LotID });
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };
        $scope.load = function (page) {
            if (page === 0 || (page > $scope.pageCount && $scope.pageCount > 0))
                return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("ProductApi", "Api")';
            $http.get(url, {
                params: {
                    pageNumber: page,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    productCode: $scope.productCode,
                    //categoryID: $scope.categoryID.value,
                    //partnerID: $scope.partnerID.value,
                    //lotID: $scope.lotID.value,
                    //status: $scope.status.value,
                    //expiredfrom: $scope.expiredfrom,
                    //expiredto: $scope.expiredto,
                }
            })
                .success(function (data) {
                    service.setCommonData($scope, data, data.Products);
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data.InnerException.InnerException);
                });
        };

        $scope.setSelected = function (id) {
            service.setSelected($scope, id);
        };

        $scope.check = function (id) {
            $scope.checkedObj[id] = !$scope.checkedObj[id];
        };

        $scope.setSelectedAll = function () {
            service.setSelectedAll($scope);
        };

        $scope.refresh = function (keyCode) {
            if (keyCode == undefined || keyCode === 13)
                $scope.load($scope.pageNumber);
        };

        $scope.clearrefresh = function () {
            //$scope.productCode = '',
            //$scope.categoryID = '',
            //$scope.partnerID = '',
            //$scope.lotID = '',
            //$scope.status = '',
            //$scope.expiredfrom = '',
            //$scope.expiredto = '',
            $scope.load($scope.pageNumber);
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "Product")';
        };

        $scope.edit = function (id) {
            if (id) {
                window.location = '@Url.Action("Detail", "Product")/' + id;
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.location = '@Url.Action("Detail", "Product")/' + selectedObj;
            }
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var deleteObj = service.getSelected($scope);
            if (deleteObj === '') return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("ProductApi", "Api")';
            $http.delete(url, {
                params: {
                    ids: deleteObj,
                    pageNumber: $scope.pageNumber,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    productCode: $scope.productCode,
                }
            }).success(function (data) {
                service.setCommonData($scope, data, data.Products);
                stoploading($('#content'));
                service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được xóa thành công!!!');
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.sort = function (ob) {
            service.setSortingValue($scope, ob);
            $scope.load($scope.pageNumber);
        };

        $scope.orderBy = 'productCode';
        $scope.orderDirection = 'ascending';
        $scope.productCode = '';
        $scope.checkedList = [];
        $scope.checkedObj = [{}];
        $scope.loadCategory();
        $scope.loadPartner();
        $scope.loadLot();
        $scope.load(1);
    });

    $scope.categories = [{}];
    $scope.partners = [{}];
    $scope.lots = [{}];

    $scope.statuscmb = [
            { name: 'Đạt', value: 'D' },
            { name: 'Không đạt', value: 'K' },
            { name: 'Mất', value: 'M' },
    ];

    $scope.statuslist = [{}];
    $scope.statuslist['D'] = 'Đạt';
    $scope.statuslist['K'] = 'Không đạt';
    $scope.statuslist['M'] = 'Mất';
</script>
<div ng-controller="ProductController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_ListViewHeader")
    <div id="content" class="content row">
        <div class="panel-body">
            <div id="search">
                <div class="row" style="margin-right: 0px">
                    <div class="col-lg-2">
                        <div class="form-group">
                            <label style="width: 110px">
                                Mã số chai
                            </label>
                            <input ng-model="productCode" ng-keypress="refresh($event.keyCode)" type="search" class="form-control" />
                        </div>
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Loại chai"},
                                {"Model", "categoryID"},
                                {"Name", "categoryID"},
                                {"Options", "categoryID.name for categoryID in categories track by categoryID.value"}
                            })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Khách hàng"},
                                {"Model", "partnerID"},
                                {"Name", "partnerID"},
                                {"Options", "partnerID.name for partnerID in partners track by partnerID.value"}
                            })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Lô"},
                                {"Model", "lotID"},
                                {"Name", "lotID"},
                                {"Options", "lotID.name for lotID in lots track by lotID.value"}
                            })
                    </div>                    
                    <div class="col-lg-2" style="clear: both">
                        @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                            {
                                {"Text", "Tình trạng"},
                                {"Model", "status"},
                                {"Name", "status"},
                                {"Options", "status.name for status in statuscmb track by status.value"}
                            })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_DatePicker", new Dictionary<string, string>
                            {
                                {"Text", "Niên hạn từ"},
                                {"Model", "expiredfrom"},
                                {"Name", "expiredfrom"}
                            })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_DatePicker", new Dictionary<string, string>
                            {
                                {"Text", "đến"},
                                {"Model", "expiredto"},
                                {"Name", "expiredto"}
                            })
                    </div>
                    @Html.Partial("_Search")
                </div>
            </div>
            @Html.Partial("_Toolbar_PagedList")
            <div class="table-responsive">
                <table id="data" class="table table-striped table-bordered table-hover">
                    <colgroup>
                        <col style="width: 50px" />
                        <col style="width: 100px" />
                        <col style="width: 100px" />
                        <col style="width: 100px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                        <col style="width: 70px" />
                    </colgroup>
                    <thead>
                        <tr>
                            <th>
                                <div>
                                    <input ng-model="selectedAll" ng-click="setSelectedAll()" style="margin-left: 10px;" class="center" type="checkbox">
                                </div>
                            </th>
                            <th ng-click="sort('Barcode')">Barcode
                                <div ng-class="orderBy != 'Barcode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('ProductCode')">Mã số chai
                                <div ng-class="orderBy != 'ProductCode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('CategoryID')">Loại chai
                                <div ng-class="orderBy != 'CategoryID' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Weight')">TLKT
                                <div ng-class="orderBy != 'Weight' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('CurrentWeight')">TLTT
                                <div ng-class="orderBy != 'CurrentWeight' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Volume')">DTKT
                                <div ng-class="orderBy != 'Volume' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('CurrentVolume')">DTTT
                                <div ng-class="orderBy != 'CurrentVolume' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('PressureTest')">AST
                                <div ng-class="orderBy != 'PressureTest' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('PressureLoad')">ASN
                                <div ng-class="orderBy != 'PressureLoad' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Status')">Tình trạng
                                <div ng-class="orderBy != 'Status' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-hide="totalItemOnPage == 0" ng-repeat="item in items" ng-click="setSelected(item.ProductID)"
                            ng-dblclick="edit(item.ProductID)">
                            <td class="center">
                                <div>
                                    <input ng-click="check(item.ProductID)" ng-model="checkedObj[item.ProductID]" class="center" type="checkbox" />
                                </div>
                            </td>
                            <td>{{item.Barcode}}</td>
                            <td>{{item.ProductCode}}</td>
                            <td>{{item.CategoryID}}</td>
                            <td>{{item.Weight}}</td>
                            <td>{{item.CurrentWeight}}</td>
                            <td>{{item.Volume}}</td>
                            <td>{{item.CurrentVolume}}</td>
                            <td>{{item.PressureTest}}</td>
                            <td>{{item.PressureLoad}}</td>
                            <td>{{item.Status}}</td>
                        </tr>
                        <tr ng-show="totalItemOnPage == 0">
                            <td colspan="100">Không có dữ liệu
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
